using System;
using System.ComponentModel;

namespace SharpMud.Server.Systems
{
	/// <summary>
	/// This member of the Server System family provides integrated threading for the work of whatever
	/// server system decides to inherit from it.
	/// </summary>
	public abstract class ThreadedServerSystem : StandardServerSystemBase
	{
		#region Private Variables
		private System.Threading.Thread _WorkThread;
		private bool _ThreadShouldBeAlive;
		#endregion

		protected abstract void WorkThreadStart();

        [Browsable(true), DisplayName("Work Thread Status"), ReadOnly(true)]
		public System.Threading.ThreadState WorkThreadStatus
		{
			get
			{
				return this._WorkThread.ThreadState;
			}
		}

		protected override void _Dispose()
		{
			_ThreadShouldBeAlive = false;
			_WorkThread = null;
		}

		protected override void _Start()
		{
			_ThreadShouldBeAlive = true;
			_WorkThread.Start();	
		}

		protected override void _Initialize()
		{
			_ThreadShouldBeAlive = false;
			_WorkThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.WorkThreadStart));
		}

		protected override void _Pause()
		{
			_WorkThread.Suspend();
		}

		protected override void _Resume()
		{
			_WorkThread.Resume();
		}

		protected override void _Stop()
		{
			_ThreadShouldBeAlive = false;
			_WorkThread.Abort();
		}

		protected bool ThreadShouldBeAlive
		{
			get
			{
				return this._ThreadShouldBeAlive;
			}
		}
	}
}
